/*IPROG - NEW - INI*/
package com.efacil.commerce.order.helpers;

import org.apache.xerces.dom.ElementNSImpl;

import com.xcustom.commerce.vo.NotaFiscalFileType;
import com.xcustom.commerce.vo.NotaFiscalVO;
import com.xcustom.order.notaFiscal.services.DANFEOutput;
import com.xcustom.order.notaFiscal.services.Download;
import com.xcustom.order.notaFiscal.services.DownloadOutput;
import com.xcustom.order.notaFiscal.services.GetDANFE;
import com.xcustom.order.notaFiscal.services.NotaFiscalEletronica;
import com.xcustom.order.notaFiscal.services.NotaFiscalEletronicaSoap;
import com.xcustom.order.notaFiscal.services.GetNotaFiscalResponse.GetNotaFiscalResult;

/**
 * @author commerce
 * External system integration eFacil.
 */
public class NotaFiscalEfacilHelper {
	
	/**
	 * @param nfvo VO with the data to perfor external service call. (eFacil integration)
	 * @return NotaFiscalFileType 
	 * Is used to return a Nota Fiscal PDF format.
	 * */
	public NotaFiscalFileType getPdf(NotaFiscalVO nfvo){
		
		NotaFiscalEletronica nfe = new NotaFiscalEletronica();
		NotaFiscalEletronicaSoap nfes =  nfe.getNotaFiscalEletronicaSoap();
		GetNotaFiscalResult nfResult =  nfes.getNotaFiscal(nfvo.getFilial(), nfvo.getNroCliente(),nfvo.getIntUser(), nfvo.getNumNotaFiscal(), nfvo.getDateIni(), nfvo.getDateFim(), nfvo.getKey());
		ElementNSImpl element = (ElementNSImpl) nfResult.getAny();
		ElementNSImpl n = (ElementNSImpl) element.getFirstChild();
		GetDANFE.Ds ds = new GetDANFE.Ds();
		ds.setAny(n);
		DANFEOutput danfeOutput =  nfes.getDANFE(ds);
		
		return new NotaFiscalFileType("pdf",danfeOutput.getArquivo(),danfeOutput.getNomeArquivo());
		
	}
	
	/**
	 * @param nfvo VO with the data to perfor external service call. (eFacil integration)
	 * @return NotaFiscalFileType 
	 * Is used to return a Nota Fiscal XML format.
	 * */
	public NotaFiscalFileType getxml(NotaFiscalVO nfvo) {
		
		NotaFiscalEletronica nfe = new NotaFiscalEletronica();
		NotaFiscalEletronicaSoap nfes =  nfe.getNotaFiscalEletronicaSoap();
		GetNotaFiscalResult nfResult =  nfes.getNotaFiscal(nfvo.getFilial(), nfvo.getNroCliente(),nfvo.getIntUser(), nfvo.getNumNotaFiscal(), nfvo.getDateIni(), nfvo.getDateFim(), nfvo.getKey());
		ElementNSImpl element = (ElementNSImpl) nfResult.getAny();
		ElementNSImpl n = (ElementNSImpl) element.getFirstChild();
		Download.Ds ds = new Download.Ds();
		ds.setAny(n);
		DownloadOutput downloadOuput   =  nfes.download(ds);
		
		return new NotaFiscalFileType("xml",downloadOuput.getArquivo(),downloadOuput.getNomeArquivo());
		
	}
}
/*IPROG - NEW - END*/